package com.system.service.impl;

import com.common.enums.BaseResponseCode;
import com.common.utils.AjaxResult;
import com.generator.entity.Tree;
import com.system.entity.SystemMenuEntity;
import com.system.entity.menu.Menu;
import com.system.entity.menu.MenuGuan;
import com.system.mapper.SystemMenuMapper;
import com.system.service.ISysRoleMenuService;
import com.system.service.SystemMenuService;
import com.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yangzhan
 */
@Service
public class SysemMenuServiceImpl implements SystemMenuService {

    @Autowired
    private SystemMenuMapper systemMenuMapper;


    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ISysRoleMenuService sysRoleMenuService;


    /**
     * 数据添加
     */

    @Override
    public AjaxResult temMenuAdd(SystemMenuEntity systemMenuEntity) {
        int n = systemMenuMapper.temMenuAdd(systemMenuEntity);
        return new AjaxResult(BaseResponseCode.ADD_SUCCESS.getCode(), BaseResponseCode.ADD_SUCCESS.getMsg());
    }

    /**
     * 删除与批量删除
     *
     * @param ids
     */
    @Override
    public AjaxResult temMenuDel(List<Integer> ids) {
        systemMenuMapper.temMenuDel(ids);
        return new AjaxResult(BaseResponseCode.DELETE_SUCCESS.getCode(), BaseResponseCode.DELETE_SUCCESS.getMsg());
    }

    /**
     * 更新
     */
    @Override
    public AjaxResult temMenuUpById(SystemMenuEntity systemMenuEntity) {
        systemMenuMapper.temMenuUpById(systemMenuEntity);
        return new AjaxResult(BaseResponseCode.UPDATE_SUCCESS.getCode(), BaseResponseCode.UPDATE_SUCCESS.getMsg());
    }


    /**
     * 查询数据库
     *
     * @return
     */
    @Override
    public AjaxResult temMenuSelectListByPage(SystemMenuEntity systemMenuEntity) {
        Integer count = systemMenuMapper.temMenuSelCount();
        List<SystemMenuEntity> list = systemMenuMapper.temMenuSelectListByPage(systemMenuEntity);
        return AjaxResult.data(count, list);
    }

    /**
     * 目录
     *
     * @return
     */
    @Override
    public Map<String, Object> menuTo(UserVO user) {

        Map<String, Object> map = new HashMap<>(16);
        Map<String, Object> home = new HashMap<>(16);
        Map<String, Object> logo = new HashMap<>(16);
        home.put("title", "首页");
        //控制器路由,自行定义
        home.put("href", "/welcome");
        logo.put("title", "YZ管理系统");
        //静态资源文件路径,可使用默认的logo.png
        logo.put("image", "image/yz.jpg");
        logo.put("href", "");
        map.put("homeInfo", home);
        map.put("logoInfo", logo);
        List<SystemMenuEntity> menuList = null;
        if (user.isAdmin()) {
            menuList = systemMenuMapper.selectMenuList();
        } else {
            menuList = systemMenuMapper.selectMenuListGo(user.getUserId());
        }

        List<Menu> menuInfo = new ArrayList<>();
        List<SystemMenuEntity> menuList0 = new ArrayList<>();
        List<SystemMenuEntity> menuList1 = new ArrayList<>();
        List<SystemMenuEntity> menuList2 = new ArrayList<>();
        for (SystemMenuEntity stemMenuEntity : menuList) {
            if (stemMenuEntity.getSort() == -2) {
                menuList0.add(stemMenuEntity);
            } else if (stemMenuEntity.getSort() == -1) {
                menuList1.add(stemMenuEntity);
            } else if (stemMenuEntity.getSort() == 0) {
                menuList2.add(stemMenuEntity);
            }
        }
        for (SystemMenuEntity e0 : menuList0) {
            Menu menu0 = new Menu();
            menu0.setId(e0.getId());
            menu0.setPid(e0.getPid());
            menu0.setHref(e0.getHref());
            menu0.setTitle(e0.getTitle());
            menu0.setIcon(e0.getIcon());
            menu0.setTarget(e0.getTarget());
            List<Menu> menus0 = new ArrayList<>();
            for (SystemMenuEntity e1 : menuList1) {
                Menu menu1 = new Menu();
                if (e0.getId().equals(e1.getPid())) {
                    menu1.setId(e1.getId());
                    menu1.setPid(e1.getPid());
                    menu1.setHref(e1.getHref());
                    menu1.setTitle(e1.getTitle());
                    menu1.setIcon(e1.getIcon());
                    menu1.setTarget(e1.getTarget());
                    menus0.add(menu1);
                }
                List<Menu> menus1 = new ArrayList<>();
                for (SystemMenuEntity e2 : menuList2) {
                    Menu menu2 = new Menu();
                    if (e1.getId().equals(e2.getPid())) {
                        menu2.setId(e2.getId());
                        menu2.setPid(e2.getPid());
                        menu2.setHref(e2.getHref());
                        menu2.setTitle(e2.getTitle());
                        menu2.setIcon(e2.getIcon());
                        menu2.setTarget(e2.getTarget());
                        menus1.add(menu2);
                    }
                }
                menu1.setChild(menus1);
            }
            menu0.setChild(menus0);
            menuInfo.add(menu0);
        }
        map.put("menuInfo", menuInfo);
        return map;

    }

    /**
     * 菜单管理列表
     *
     * @return
     */
    @Override
    public AjaxResult menuGuan() {

        HashMap<String, Object> map = new HashMap<>();
        Object men = redisTemplate.opsForValue().get("men");
        if (true) {
            List<SystemMenuEntity> menuList = systemMenuMapper.selectMenuListTo();
            Integer count = systemMenuMapper.temMenuSelCount();
            List<MenuGuan> menuGuanList = new ArrayList<>();
            for (SystemMenuEntity menu : menuList) {
                MenuGuan menuGuan = new MenuGuan();
                menuGuan.setAuthorityId(menu.getId());
                menuGuan.setAuthorityName(menu.getTitle());
                menuGuan.setOrderNumber(menu.getId());
                menuGuan.setMenuUrl(menu.getHref());
                menuGuan.setMenuIcon(menu.getIcon());
                menuGuan.setCreateTime(menu.getCreateAt());
                menuGuan.setAuthority(menu.getPerms());
                menuGuan.setChecked(0);
                menuGuan.setUpdateTime(menu.getUpdateAt());
                menuGuan.setIsMenu(menu.getSort());
                menuGuan.setParentId(menu.getPid());
                menuGuan.setStatus(menu.getStatus());
                menuGuanList.add(menuGuan);
            }
            map.put("count", count);
            map.put("menuGuanList", menuGuanList);
            redisTemplate.opsForValue().set("men", map);
            return new AjaxResult(0, menuGuanList);
        }
        HashMap<String, Object> map1 = (HashMap<String, Object>) men;
        System.out.println("map1 = " + map1);
        return new AjaxResult((Integer) map1.get("count"), map1.get("menuGuanList"));
    }


    /**
     * 角色表添加菜单
     *
     * @return
     */
    @Override
    public Map<String, Object> getTree() {
        List<SystemMenuEntity> menuList = systemMenuMapper.selectMenuList();
        List<SystemMenuEntity> menuList_2 = new ArrayList<>();
        List<SystemMenuEntity> menuList_1 = new ArrayList<>();
        List<SystemMenuEntity> menuList0 = new ArrayList<>();
        List<SystemMenuEntity> menuList1 = new ArrayList<>();
        List<SystemMenuEntity> menuList2 = new ArrayList<>();
        //全选
        List<Integer> listt = new ArrayList<>();
        for (SystemMenuEntity stemMenuEntity : menuList) {
            listt.add(stemMenuEntity.getId());
            if (stemMenuEntity.getSort() == -1) {
                menuList_1.add(stemMenuEntity);
            } else if (stemMenuEntity.getSort() == 0) {
                menuList0.add(stemMenuEntity);
            } else if (stemMenuEntity.getSort() == 1 || stemMenuEntity.getSort() == 2) {
                menuList1.add(stemMenuEntity);
            } else if (stemMenuEntity.getSort() == 3) {
                menuList2.add(stemMenuEntity);
            } else if (stemMenuEntity.getSort() == -2) {
                menuList_2.add(stemMenuEntity);
            }
        }
        List<Tree> list = new ArrayList<Tree>();
        //第一层

        for (SystemMenuEntity stemMenuEntity_2 : menuList_2) {
            List<Tree> list0 = new ArrayList<Tree>();
            Tree tree0 = new Tree();
            tree0.setTitle(stemMenuEntity_2.getTitle());
            tree0.setId(stemMenuEntity_2.getId());
            for (SystemMenuEntity stemMenuEntity : menuList_1) {
                List<Tree> list1 = new ArrayList<Tree>();
                Tree tree = new Tree();
                if (stemMenuEntity_2.getId().equals(stemMenuEntity.getPid())) {

                    tree.setTitle(stemMenuEntity.getTitle());
                    tree.setId(stemMenuEntity.getId());
                    //第二层
                    for (SystemMenuEntity stemMenuEntity1 : menuList0) {
                        List<Tree> list2 = new ArrayList<Tree>();
                        Tree tree1 = new Tree();
                        if (stemMenuEntity.getId().equals(stemMenuEntity1.getPid())) {
                            if (stemMenuEntity1.getPerms() != null) {
                                tree1.setTitle(stemMenuEntity1.getTitle() + "  " + stemMenuEntity1.getPerms());
                            } else {
                                tree1.setTitle(stemMenuEntity1.getTitle());
                            }
                            tree1.setId(stemMenuEntity1.getId());
                        }
                        //第三层
                        for (SystemMenuEntity stemMenuEntity2 : menuList1) {
                            List<Tree> list3 = new ArrayList<Tree>();
                            Tree tree2 = new Tree();
                            if (stemMenuEntity1.getId().equals(stemMenuEntity2.getPid())) {
                                if (stemMenuEntity2.getPerms() != null) {
                                    tree2.setTitle(stemMenuEntity2.getTitle() + "  " + stemMenuEntity2.getPerms());
                                } else {
                                    tree2.setTitle(stemMenuEntity2.getTitle());
                                }
                                tree2.setId(stemMenuEntity2.getId());
                            }
                            //第四层
                            for (SystemMenuEntity systemMenuEntity3 : menuList2) {
                                Tree tree3 = new Tree();
                                if (stemMenuEntity2.getId().equals(systemMenuEntity3.getPid())) {
                                    if (systemMenuEntity3.getPerms() != null) {
                                        tree3.setTitle(systemMenuEntity3.getTitle() + "  " + systemMenuEntity3.getPerms());
                                    } else {
                                        tree3.setTitle(systemMenuEntity3.getTitle());
                                    }
                                    tree3.setId(systemMenuEntity3.getId());
                                }
                                if (stemMenuEntity2.getId().equals(systemMenuEntity3.getPid())) {
                                    list3.add(tree3);
                                }
                            }
                            if (stemMenuEntity1.getId().equals(stemMenuEntity2.getPid())) {
                                tree2.setChildren(list3);
                                list2.add(tree2);
                            }
                        }
                        if (stemMenuEntity.getId().equals(stemMenuEntity1.getPid())) {
                            tree1.setChildren(list2);
                            list1.add(tree1);
                        }
                    }
                    tree.setChildren(list1);
                    list0.add(tree);
                }
            }

            tree0.setChildren(list0);
            list.add(tree0);
        }

        Map<String, Object> map = new HashMap<>();
        map.put("li", listt);
        map.put("list", list);
        return map;
    }

    /**
     * 角色表修改菜单
     *
     * @return
     */
    @Override
    public Map<String, Object> getTreeUp(Long id) {
        List<Long> longs = sysRoleMenuService.getMenuId(id);
        List<SystemMenuEntity> menuList = systemMenuMapper.selectMenuList();
        for (SystemMenuEntity tt : menuList) {
            for (Long t : longs) {
                if (t.equals(Long.valueOf(tt.getId()))) {
                    tt.setChecked(true);
                    break;
                }
            }
        }
        List<SystemMenuEntity> menuList_2 = new ArrayList<>();
        List<SystemMenuEntity> menuList_1 = new ArrayList<>();
        List<SystemMenuEntity> menuList0 = new ArrayList<>();
        List<SystemMenuEntity> menuList1 = new ArrayList<>();
        List<SystemMenuEntity> menuList2 = new ArrayList<>();
        //全选
        List<Integer> listt = new ArrayList<>();
        for (SystemMenuEntity stemMenuEntity : menuList) {
            listt.add(stemMenuEntity.getId());
            if (stemMenuEntity.getSort() == -1) {
                menuList_1.add(stemMenuEntity);
            } else if (stemMenuEntity.getSort() == 0) {
                menuList0.add(stemMenuEntity);
            } else if (stemMenuEntity.getSort() == 1 || stemMenuEntity.getSort() == 2) {
                menuList1.add(stemMenuEntity);
            } else if (stemMenuEntity.getSort() == 3) {
                menuList2.add(stemMenuEntity);
            } else if (stemMenuEntity.getSort() == -2) {
                menuList_2.add(stemMenuEntity);
            }
        }
        List<Tree> list = new ArrayList<Tree>();
        //第一层
        for (SystemMenuEntity stemMenuEntity_2 : menuList_2) {
            List<Tree> list0 = new ArrayList<Tree>();
            Tree tree0 = new Tree();
            tree0.setTitle(stemMenuEntity_2.getTitle());
            tree0.setId(stemMenuEntity_2.getId());
            for (SystemMenuEntity stemMenuEntity : menuList_1) {
                List<Tree> list1 = new ArrayList<Tree>();
                Tree tree = new Tree();
                if (stemMenuEntity_2.getId().equals(stemMenuEntity.getPid())) {
                    tree.setTitle(stemMenuEntity.getTitle());
                    tree.setId(stemMenuEntity.getId());
                    //第二层
                    for (SystemMenuEntity stemMenuEntity1 : menuList0) {
                        List<Tree> list2 = new ArrayList<Tree>();
                        Tree tree1 = new Tree();
                        if (stemMenuEntity.getId().equals(stemMenuEntity1.getPid())) {
                            if (stemMenuEntity1.getPerms() != null) {
                                tree1.setTitle(stemMenuEntity1.getTitle() + "  " + stemMenuEntity1.getPerms());
                            } else {
                                tree1.setTitle(stemMenuEntity1.getTitle());
                            }
                            tree1.setChecked(stemMenuEntity1.isChecked());
                            tree1.setId(stemMenuEntity1.getId());
                        }
                        //第三层
                        for (SystemMenuEntity stemMenuEntity2 : menuList1) {
                            List<Tree> list3 = new ArrayList<Tree>();
                            Tree tree2 = new Tree();
                            if (stemMenuEntity1.getId().equals(stemMenuEntity2.getPid())) {
                                if (stemMenuEntity2.getPerms() != null) {
                                    tree2.setTitle(stemMenuEntity2.getTitle() + "  " + stemMenuEntity2.getPerms());
                                } else {
                                    tree2.setTitle(stemMenuEntity2.getTitle());
                                }
                                tree2.setChecked(stemMenuEntity2.isChecked());
                                tree2.setId(stemMenuEntity2.getId());
                            }
                            //第四层
                            for (SystemMenuEntity systemMenuEntity3 : menuList2) {
                                Tree tree3 = new Tree();
                                if (stemMenuEntity2.getId().equals(systemMenuEntity3.getPid())) {
                                    if (systemMenuEntity3.getPerms() != null) {
                                        tree3.setTitle(systemMenuEntity3.getTitle() + "  " + systemMenuEntity3.getPerms());
                                    } else {
                                        tree3.setTitle(systemMenuEntity3.getTitle());
                                    }
                                    tree3.setChecked(systemMenuEntity3.isChecked());
                                    tree3.setId(systemMenuEntity3.getId());
                                }
                                if (stemMenuEntity2.getId().equals(systemMenuEntity3.getPid())) {
                                    list3.add(tree3);
                                }
                            }
                            if (stemMenuEntity1.getId().equals(stemMenuEntity2.getPid())) {
                                if (list3.size() < 1) {
                                    tree2.setChecked(stemMenuEntity2.isChecked());
                                } else {
                                    tree2.setChecked(false);
                                }
                                tree2.setChildren(list3);
                                list2.add(tree2);
                            }
                        }
                        if (stemMenuEntity.getId().equals(stemMenuEntity1.getPid())) {
                            if (list2.size() < 1) {
                                tree1.setChecked(stemMenuEntity1.isChecked());
                            } else {
                                tree1.setChecked(false);
                            }
                            tree1.setChildren(list2);
                            list1.add(tree1);
                        }
                    }
                    if (list1.size() < 1) {
                        tree.setChecked(stemMenuEntity.isChecked());
                    } else {
                        tree.setChecked(false);
                    }
                    tree.setChildren(list1);
                    list0.add(tree);
                }
            }
            if (list0.size() < 1) {
                tree0.setChecked(stemMenuEntity_2.isChecked());
            } else {
                tree0.setChecked(false);
            }
            tree0.setChildren(list0);
            list.add(tree0);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("li", listt);
        map.put("list", list);
        return map;
    }
}